#include <stdio.h>
#include <stdlib.h>

int main(void) {
    while (1) {
        int n;
        scanf("%d", &n);
        if (n == 0) {
            break;
        }
        if (n == 1) {
            int num;
            scanf("%d", &num);
            printf("%d\n", num % 2013);
            continue;
        }
        int* array = (int*) calloc(n, sizeof(int));
        int* tmp_array = (int*) calloc(n - 1, sizeof(int));
        for (int i = 0; i < n; i++) {
            scanf("%d", &array[i]);
        }
        for (int x = n - 1; x > 0; x--) {
            for(int y = 0; y < x; y++) {
                tmp_array[y] = (array[y] + array[y + 1]) % 2013;
            }
            int* tmp = tmp_array;
            tmp_array = array;
            array = tmp;
        }
        printf("%d\n", array[0]);
        free(array);
        free(tmp_array);
    }
    return 0;
}
